bitmasks constructive algorithms *1900

Please click on ads to support us..

Python Code:

import sys, os, io
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

n, x = map(int, input().split())
pow2 = [1]
for _ in range(n):
    pow2.append(2 * pow2[-1])
pn = pow2[n]
if x >= pn:
    l = pn - 1
    ans = [i for i in range(1, pn)]
    for i in range(l - 1, 0, -1):
        ans[i] ^= ans[i - 1]
elif n == 1:
    l = 0
else:
    ok = [1] * pn
    ok[x] = 0
    if x ^ 1:
        ans = [1]
        ok[1], ok[1 ^ x] = 0, 0
    else:
        ans = [2]
        ok[2], ok[2 ^ x] = 0, 0
    i = 1
    while i < pn:
        if not ok[i]:
            i += 1
        else:
            ans.append(i)
            ok[i] = 0
            ok[i ^ x] = 0
    l = len(ans)
    for i in range(l - 1, 0, -1):
        ans[i] ^= ans[i - 1]
print(l)
if l:
    sys.stdout.write(" ".join(map(str, ans)))

C++ Code:

#include <iostream>
#include <bits/stdc++.h> // Standard Template Library
#include <math.h>
 
#define ll long long
#define ld long double
#define int ll
#define vi vector<int>
#define pii pair<int,int>
#define f first
#define se second
#define vii vector<pii>
#define setBits(x) builtin_popcount(x)
#define F(a, i) for (int i = 0; i < a; i++)
#define FN(a, b, i) for (int i = a; i >= b; i--)
const ll M = 1e9 + 7;
const ll md = 998244353;
 
using namespace std;
// using namespace chrono;

// DEBUG FUNCTIONS 
#ifndef ONLINE_JUDGE
 
template<typename T>
void __p(T a) {
    cout<<a;
}
template<typename T, typename F>
void __p(pair<T, F> a) {
    cout<<"{";
    __p(a.first);
    cout<<",";
    __p(a.second);
    cout<<"}";
}
template<typename T>
void __p(std::vector<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it<a.end(); it++)
        __p(*it),cout<<",}"[it+1==a.end()];
}
template<typename T>
void __p(std::set<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it!=a.end();){
        __p(*it); 
        cout<<",}"[++it==a.end()];
    }
 
}
template<typename T>
void __p(std::multiset<T> a) {
    cout<<"{";
    for(auto it=a.begin(); it!=a.end();){
        __p(*it); 
        cout<<",}"[++it==a.end()];
    }
}
template<typename T, typename F>
void __p(std::map<T,F> a) {
    cout<<"{\n";
    for(auto it=a.begin(); it!=a.end();++it)
    {
        __p(it->first);
        cout << ": ";
        __p(it->second);
        cout<<"\n";
    }
    cout << "}\n";
}
 
template<typename T, typename ...Arg>
void __p(T a1, Arg ...a) {
    __p(a1);
    __p(a...);
}
template<typename Arg1>
void __f(const char *name, Arg1 &&arg1) {
    cout<<name<<" : ";
    __p(arg1);
    cout<<endl;
}
template<typename Arg1, typename ... Args>
void __f(const char *names, Arg1 &&arg1, Args &&... args) {
    int bracket=0,i=0;
    for(;; i++)
        if(names[i]==','&&bracket==0)
            break;
        else if(names[i]=='(')
            bracket++;
        else if(names[i]==')')
            bracket--;
    const char *comma=names+i;
    cout.write(names,comma-names)<<" : ";
    __p(arg1);
    cout<<" | ";
    __f(comma+1,args...);
}
#define trace(...) cout<<"Line:"<<__LINE__<<" ", __f(#__VA_ARGS__, __VA_ARGS__)
#else
#define trace(...)
#define error(...)
#endif
 
 
// DEBUG FUNCTIONS END 

void solve(){
    ll n, x; cin>>n>>x;
    if(n==1 && x==1){
        cout<<0<<endl;
        return;
    }
    ll z = 1<<n;
    //trace(z);
    if(x>=z){
        cout<<z-1<<endl;
        ll m = z-1;
        vector<ll> v(m);
        vector<ll> sufxor(m);
        F(m,i) sufxor[i] = m-i;
        //trace(sufxor);
        v[m-1] = sufxor[m-1];
        for(int i=0; i<m-1; i++){
            v[i] = sufxor[i]^sufxor[i+1];
        }
        for(int i=0; i<m; i++) cout<<v[i]<<" ";
        return;
    }
    cout<<(z-2)/2<<endl;
    ll m = (z-2)/2;
    vector<ll> v(m);
    vector<ll> sufxor(m);
    ll cnt=0;
    vector<bool> str;
    ll ptr =1;
    for(int i=0; i<z; i++) str.push_back(true);
    while(cnt<m){
        if(str[ptr] && str[ptr^x] && ptr!=x){
            sufxor[cnt] = ptr;
            cnt++;
            str[ptr] = false;
            str[ptr^x] = false;
        }
        ptr++;
    }
    //trace(sufxor);
    v[m-1] = sufxor[m-1];
    for(int i=0; i<m-1; i++){
        v[i] = sufxor[i]^sufxor[i+1];
    }
    for(int i=0; i<m; i++) cout<<v[i]<<" ";
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); 
    ll t = 1;// cin >> t;
    while(t--)solve();
    return 0;
}


Comments

Submit
0 Comments
More Questions

394. Decode String
902. Numbers At Most N Given Digit Set
221. Maximal Square
1200. Minimum Absolute Difference
1619B - Squares and Cubes
1619A - Square String
1629B - GCD Arrays
1629A - Download More RAM
1629C - Meximum Array
1629D - Peculiar Movie Preferences
1629E - Grid Xor
1629F1 - Game on Sum (Easy Version)
2148. Count Elements With Strictly Smaller and Greater Elements
2149. Rearrange Array Elements by Sign
2150. Find All Lonely Numbers in the Array
2151. Maximum Good People Based on Statements
2144. Minimum Cost of Buying Candies With Discount
Non empty subsets
1630A - And Matching
1630B - Range and Partition
1630C - Paint the Middle
1630D - Flipping Range
1328A - Divisibility Problem
339A - Helpful Maths
4A - Watermelon
476A - Dreamoon and Stairs
1409A - Yet Another Two Integers Problem
977A - Wrong Subtraction
263A - Beautiful Matrix
180C - Letter